﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dominio;

namespace Persistencia
{
    class BrokerIVA: Broker
    {
        #region constructor

        public BrokerIVA()
        {
            this.conn = ConexionBD.getInstance();
        }

        #endregion

        #region methods

        public override void add(Object o)
        {
            IVA IVAAux = (IVA)o;
            this.conn.connect();
            string sql = "insert into IVAs(Nombre, porcentaje) values('"
                + IVAAux.Nombre + "', " + IVAAux.Porcentaje + ")";
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void update(Object o)
        {
            IVA IVAAux = (IVA)o;
            string sql = "update IVAs set Nombre = '" + IVAAux.Nombre
                + "', porcentaje = " + IVAAux.Porcentaje
                + " where idIVA = " + IVAAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void delete(Object o)
        {
            IVA IVAAux = (IVA)o;
            string sql = "delete * from IVAs where IdIVA = " + IVAAux.OID;
            this.conn.connect();
            this.conn.execute(sql);
            this.conn.disconnect();
        }

        public override void bringData(Object o)
        {
            IVA IVAAux = (IVA)o;
            this.conn.connect();
            string sql = "Select * from IVAs where IdIVA = " + IVAAux.OID;
            this.conn.bringData(sql);
            while (this.conn.nextRecord())
            {
                IVAAux.Nombre = this.conn.getDataString("Nombre").ToString();
                IVAAux.Porcentaje = int.Parse(this.conn.getDataString("Porcentaje").ToString());
            }
            this.conn.disconnect();
        }

        public override List<Object> bringList(Object o)
        {
            string sql = "Select * from IVAs";
            List<Object> list = new List<Object>();
            this.conn.connect();
            this.conn.bringList(sql);
            while (this.conn.nextRecord())
            {
                IVA IVAAux = new IVA();
                IVAAux.Nombre = this.conn.getDataString("Nombre").ToString();
                IVAAux.Porcentaje = int.Parse(this.conn.getDataString("Porcentaje").ToString());
                IVAAux.OID = int.Parse(this.conn.getDataString("IdIVA").ToString());
                list.Add(IVAAux);
            }
            this.conn.disconnect();
            return list;
        }

        public override int bringLastId()
        {
            int id = 0;
            try
            {
                this.conn.connect();
                string sql = "Select max(idIVA) as idIVA from IVAs";
                this.conn.bringData(sql);
                while (this.conn.nextRecord())
                {
                    id = int.Parse(this.conn.getDataString("idIVA").ToString());
                }
                this.conn.disconnect();
            }
            catch (Exception)
            {
                this.conn.disconnect();
                id = 0;
            }            
            return id;
        }

        #endregion
    }
}
